import asyncio
import time
from ygg_aiomysql.db import new_pool
from ygg_aiomysql.database import add_database

async def main():
    """
    示例04：手动创建不存在的数据库
    """
    # 使用时间戳生成唯一数据库名
    test_db_name = f"test_db_{int(time.time())}"
    
    print(f"开始创建数据库: {test_db_name}")
    
    # 创建临时连接池
    pool = await new_pool(password="zhangdapeng520")
    
    # 调用方法创建数据库
    await add_database(pool, test_db_name)
    print(f"数据库 {test_db_name} 创建成功")
    
    # 验证数据库已创建
    async with pool.acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute("SHOW DATABASES")
            databases = [db[0] for db in await cur.fetchall()]
            assert test_db_name in databases
            print(f"验证成功: 数据库 {test_db_name} 已存在")
    
    # 关闭连接池
    pool.close()
    await pool.wait_closed()
    
    # 清理测试数据库
    async with (await new_pool(password="zhangdapeng520")).acquire() as conn:
        async with conn.cursor() as cur:
            await cur.execute(f"DROP DATABASE {test_db_name}")
            print(f"已清理测试数据库: {test_db_name}")

if __name__ == "__main__":
    asyncio.run(main())